#ifndef CAN_THREAD_H
#define CAN_THREAD_H

#include <QThread>
#include <QQueue>
#include <QMutex>
#include <QWaitCondition>
#include <linux/can.h>
#include <linux/can/raw.h>

Q_DECLARE_METATYPE(can_frame)

class CanThread : public QThread {
    Q_OBJECT
public:
    explicit CanThread(const QString &interfaceName, QObject *parent = nullptr);
    ~CanThread();

    bool stopThread;
    // 添加消息到队列
    void enqueueMessage(const struct can_frame &frame);
    void enqueueMessage_1( struct can_frame frame);
    bool sendAttitude( float yaw, float pitch, float roll);
    bool sendOffset(float x_offset, float y_offset, float z_offset, bool CamHkAck);
    bool m_isRun;
    QString can_name;
    bool isRun(){return m_isRun;}
signals:
    // 信号：接收到新消息
    void messageReceived(const struct can_frame frame);
    void MCU_Trans(float x, float y, float z);
    void MCU_Rot(float yaw, float pitch);
protected:
    // 线程主函数
    void run() override;

private:
    // 初始化 CAN 设备
    bool initCanDevice(const QString &interfaceName);

    int canSocket; // CAN socket 描述符
    QString interfaceName; // CAN 接口名称（如 can0）

    QQueue<struct can_frame> messageQueue; // 消息队列
    QMutex queueMutex; // 队列互斥锁
    QWaitCondition queueNotEmpty; // 队列非空条件变量

    void parseCanMessage( struct can_frame &frame);
};

#endif // CAN_THREAD_H
